package site.kitcheng.springboottest.infra.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * ClassName: FileSHA256
 * Package: site.kitcheng.springboottest.infra.utils
 * Description:
 *
 * @Author jiangCheng
 * @Create 2025/2/7 14:25
 * @Version 1.0
 */
public class FileSHA256 {
    public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
      File f=  new File("D:\\workspace\\jingao\\springboot-test\\upload\\ea2c9eb4605d095952e05253e70d33ed01143e4a431be2b1c2bfa8b13b819ec1\\Doubao_installer.exe");
      DataInputStream dis = new DataInputStream(new FileInputStream(f));
        byte[] hash = computeSHA256(dis.readAllBytes());
        System.out.println("SHA-256 哈希值: " + bytesToHex(hash));

        String s = DigestUtils.sha256Hex(new FileInputStream(f));
        System.out.println("SHA-256 哈希值: " + s);

    }

    public static byte[] computeSHA256(byte[] data) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return digest.digest(data);
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }




}
